//
//  YCCommentView.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/15.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCCommentView.h"
@interface YCCommentView()<UITextViewDelegate,UITextFieldDelegate>

@property (nonatomic,readonly) UIView *maskView;
@property (nonatomic,readonly) UIView *accessoryView;
@property (nonatomic,readonly) UITextView *textView;
@property (nonatomic,readonly) UITextField *textField;
@property (nonatomic,readonly) UIButton *cancelButton;
@property (nonatomic,readonly) UIButton *sendButton;
@property (nonatomic,copy) YCCommentViewCancelBlock cancel;
@property (nonatomic,copy) YCCommentViewSendBlock send;

@end
@implementation YCCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    if(self = [super initWithFrame:frame])
    {
        self.tintColor = [UIColor blackColor];
        
        _maskView = [[UIView alloc] initWithFrame:self.bounds];
        _maskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFromGestureRecognizer:)];
        [_maskView addGestureRecognizer:tapGestureRecognizer];
        
        [self addSubview:_maskView];
        
        CGFloat height = (kScreenWidth - 30) * 77 / 290.0;
        
        _accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight , kScreenWidth, height + 44.0 + 18.0)];
        _accessoryView.backgroundColor = [UIColor yc_colorWithUInt:0xeeeeee];
        
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 44.0, kScreenWidth - 30, height)];
        _textView.layer.borderColor = [UIColor yc_colorWithUInt:0xc0c0c0].CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.returnKeyType = UIReturnKeySend;
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.delegate = self;
        _textView.enablesReturnKeyAutomatically = YES;
        
        [_accessoryView addSubview:_textView];
        
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0, 44.0)];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor yc_colorWithUInt:0xacacac] forState:UIControlStateDisabled];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancelButton addTarget:self action:@selector(didTouchUpInsideCancelButton:) forControlEvents:UIControlEventTouchUpInside];
        [_accessoryView addSubview:_cancelButton];
        
        _sendButton = [[UIButton alloc] initWithFrame:CGRectMake(self.yc_width - 60.0, 0, 60.0, 44.0)];
        [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
        [_sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_sendButton setTitleColor:[UIColor yc_colorWithUInt:0xacacac] forState:UIControlStateDisabled];
        [_sendButton addTarget:self action:@selector(didTouchUpInsideSendButton:) forControlEvents:UIControlEventTouchUpInside];
        _sendButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sendButton.enabled = NO;
        [_accessoryView addSubview:_sendButton];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.yc_width - 100) / 2.0, 0, 100, 44.0)];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [_accessoryView addSubview:_titleLabel];
        
        [self addSubview:_accessoryView];
        
        [self addKeyboardObserver];
    }
    return self;
}

- (void)dealloc
{
    YCLog(@"%@ release",NSStringFromClass([self class]));
    [self removeKeyboardObserver];
}

- (void)addKeyboardObserver
{
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveKeyboardWillShowNotification:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveKeyboardWillHideNotification:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)didReceiveKeyboardWillShowNotification:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat height = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    [UIView animateWithDuration:duration animations:^{
        self.accessoryView.transform = CGAffineTransformMakeTranslation(0, -height-self.accessoryView.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)didReceiveKeyboardWillHideNotification:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
        self.accessoryView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)removeKeyboardObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)showWithSendBlock:(YCCommentViewSendBlock)block cancel:(YCCommentViewCancelBlock)cancel
{
    UIView *view = [UIApplication sharedApplication].delegate.window.rootViewController.view;
    
    self.cancel = cancel;
    self.send = block;
    
    [view addSubview:self];
    
    [self.textView becomeFirstResponder];
}

- (void)didTapFromGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    [self.cancelButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (void)didTouchUpInsideCancelButton:(UIButton *)cancelButton
{
    if(self.cancel) self.cancel();
    
    [self.textView resignFirstResponder];
}

- (void)didTouchUpInsideSendButton:(UIButton *)sendButton
{
    if(self.send) self.send(self.textView.text.yc_trimString);
    [self.textView resignFirstResponder];
}

#pragma mark - UITextView delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
    {
        [self.sendButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    self.sendButton.enabled = textView.text.length > 0;
}

#pragma mark - UITextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.textView becomeFirstResponder];
}


@end
